Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./year2.RDS")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.25, n = 510)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 11.36842 11.42484 11.48027 11.53472 11.58819 11.64068 11.69222 11.74279
##   [9] 11.79242 11.84109 11.88883 11.93564 11.98152 12.02649 12.07054 12.11368
##  [17] 12.15592 12.19727 12.23773 12.27731 12.31600 12.35378 12.39063 12.42655
##  [25] 12.46152 12.49553 12.52857 12.56063 12.59170 12.62176 12.65081 12.67883
##  [33] 12.70582 12.73175 12.75677 12.78100 12.80441 12.82696 12.84863 12.86939
##  [41] 12.88920 12.90804 12.92587 12.94267 12.95841 12.97305 12.98657 12.99893
##  [49] 13.01011 13.02008 13.02873 13.03605 13.04208 13.04687 13.05050 13.05301
##  [57] 13.05445 13.05490 13.05440 13.05300 13.05077 13.04777 13.04404 13.03965
##  [65] 13.03315 13.02327 13.01034 12.99468 12.97662 12.95649 12.93461 12.91131
##  [73] 12.88691 12.86174 12.83612 12.81038 12.78484 12.75984 12.73569 12.71272
##  [81] 12.69125 12.67163 12.65415 12.63623 12.61538 12.59213 12.56700 12.54052
##  [89] 12.51320 12.48558 12.45818 12.43152 12.40612 12.38251 12.36122 12.34276
##  [97] 12.32766 12.31428 12.30069 12.28700 12.27334 12.25981 12.24653 12.23361
## [105] 12.22117 12.20933 12.19819 12.18788 12.17850 12.17017 12.16301 12.15751
## [113] 12.15392 12.15199 12.15145 12.15204 12.15352 12.15563 12.15810 12.16068
## [121] 12.16311 12.16513 12.16649 12.16693 12.16619 12.16471 12.16315 12.16160
## [129] 12.16011 12.15877 12.15766 12.15686 12.15643 12.15645 12.15700 12.15816
## [137] 12.16000 12.16260 12.16603 12.17038 12.17570 12.18223 12.19005 12.19906
## [145] 12.20916 12.22024 12.23219 12.24492 12.25831 12.27227 12.28669 12.30147
## [153] 12.31650 12.33167 12.34689 12.36205 12.37704 12.39176 12.40611 12.41999
## [161] 12.43328 12.44588 12.45941 12.47537 12.49354 12.51365 12.53547 12.55876
## [169] 12.58327 12.60876 12.63499 12.66170 12.68866 12.71562 12.74235 12.76859
## [177] 12.79410 12.81864 12.84197 12.86384 12.88401 12.90223 12.91827 12.93187
## [185] 12.94279 12.95080 12.95564 12.95707 12.95660 12.95581 12.95460 12.95286
## [193] 12.95049 12.94738 12.94342 12.93850 12.93253 12.92539 12.91698 12.90719
## [201] 12.89592 12.88306 12.86636 12.84409 12.81692 12.78552 12.75056 12.71271
## [209] 12.67263 12.63101 12.58850 12.54578 12.50352 12.46238 12.42304 12.38616
## [217] 12.35241 12.32247 12.29113 12.25346 12.21056 12.16354 12.11348 12.06149
## [225] 12.00867 11.95612 11.90492 11.85620 11.81103 11.77052 11.73578 11.70788
## [233] 11.68282 11.65616 11.62837 11.59996 11.57140 11.54319 11.51581 11.48975
## [241] 11.46549 11.44354 11.42436 11.40846 11.39632 11.38814 11.38356 11.38217
## [249] 11.38358 11.38739 11.39320 11.40062 11.40925 11.41869 11.42855 11.43842
## [257] 11.44792 11.45664 11.46419 11.47017 11.47675 11.48617 11.49816 11.51243
## [265] 11.52872 11.54674 11.56622 11.58689 11.60846 11.63066 11.65321 11.67583
## [273] 11.69826 11.72020 11.74139 11.76155 11.78040 11.79767 11.81307 11.82834
## [281] 11.84521 11.86345 11.88279 11.90299 11.92380 11.94497 11.96625 11.98739
## [289] 12.00814 12.02825 12.04748 12.06557 12.08227 12.09807 12.11361 12.12890
## [297] 12.14395 12.15878 12.17340 12.18780 12.20201 12.21604 12.22988 12.24356
## [305] 12.25708 12.27045 12.28368 12.29679 12.30978 12.32265 12.33543 12.34812
## [313] 12.36073 12.37327 12.38598 12.39903 12.41235 12.42585 12.43947 12.45312
## [321] 12.46673 12.48021 12.49349 12.50650 12.51915 12.53138 12.54309 12.55421
## [329] 12.56538 12.57720 12.58955 12.60230 12.61535 12.62856 12.64181 12.65499
## [337] 12.66797 12.68064 12.69287 12.70454 12.71553 12.72572 12.73498 12.74321
## [345] 12.75041 12.75676 12.76232 12.76718 12.77142 12.77513 12.77838 12.78125
## [353] 12.78384 12.78621 12.78845 12.79065 12.79288 12.79522 12.79731 12.79876
## [361] 12.79965 12.80003 12.79999 12.79960 12.79891 12.79800 12.79693 12.79579
## [369] 12.79463 12.79352 12.79254 12.79175 12.78995 12.78614 12.78067 12.77390
## [377] 12.76620 12.75791 12.74940 12.74103 12.73315 12.72612 12.72031 12.71607
## [385] 12.71376 12.71373 12.71548 12.71817 12.72168 12.72592 12.73077 12.73612
## [393] 12.74185 12.74787 12.75406 12.76032 12.76652 12.77256 12.77834 12.78374
## [401] 12.78865 12.79296 12.79657 12.79935 12.80121 12.80310 12.80593 12.80951
## [409] 12.81365 12.81817 12.82288 12.82759 12.83211 12.83626 12.83984 12.84268
## [417] 12.84459 12.84537 12.84484 12.84346 12.84185 12.84002 12.83800 12.83579
## [425] 12.83341 12.83090 12.82826 12.82552 12.82269 12.81980 12.81685 12.81388
## [433] 12.81090 12.80711 12.80183 12.79525 12.78758 12.77901 12.76973 12.75994
## [441] 12.74983 12.73960 12.72945 12.71956 12.71014 12.70138 12.69348 12.68663
## [449] 12.68102 12.67578 12.66995 12.66362 12.65689 12.64985 12.64260 12.63522
## [457] 12.62782 12.62048 12.61330 12.60638 12.59980 12.59366 12.58805 12.58307
## [465] 12.57881 12.57537 12.57283 12.57129 12.57033 12.56946 12.56873 12.56818
## [473] 12.56784 12.56775 12.56795 12.56847 12.56936 12.57064 12.57237 12.57457
## [481] 12.57728 12.58054 12.58426 12.58832 12.59272 12.59745 12.60253 12.60794
## [489] 12.61368 12.61976 12.62618 12.63293 12.64001 12.64742 12.65517 12.66325
## [497] 12.67167 12.68046 12.68961 12.69911 12.70897 12.71917 12.72972 12.74060
## [505] 12.75182 12.76337 12.77525 12.78745 12.79997 12.81280
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.25, n = 510)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 10.89670 10.97031 11.04271 11.11389 11.18383 11.25253 11.31997 11.38615
##   [9] 11.45105 11.51465 11.57696 11.63795 11.69762 11.75596 11.81294 11.86857
##  [17] 11.92283 11.97571 12.02719 12.07728 12.12595 12.17324 12.21917 12.26376
##  [25] 12.30705 12.34905 12.38979 12.42930 12.46760 12.50472 12.54067 12.57550
##  [33] 12.60922 12.64185 12.67310 12.70268 12.73068 12.75715 12.78216 12.80578
##  [41] 12.82808 12.84912 12.86898 12.88773 12.90542 12.92213 12.93792 12.95287
##  [49] 12.96704 12.98050 12.99219 13.00118 13.00775 13.01218 13.01475 13.01575
##  [57] 13.01545 13.01414 13.01208 13.00957 13.00689 13.00431 13.00211 13.00058
##  [65] 12.99883 12.99585 12.99171 12.98648 12.98021 12.97299 12.96486 12.95591
##  [73] 12.94620 12.93580 12.92476 12.91317 12.90109 12.88857 12.87571 12.86255
##  [81] 12.84916 12.83562 12.82199 12.80666 12.78829 12.76734 12.74428 12.71955
##  [89] 12.69362 12.66695 12.64000 12.61322 12.58708 12.56203 12.53854 12.51707
##  [97] 12.49806 12.47826 12.45455 12.42765 12.39826 12.36709 12.33486 12.30226
## [105] 12.27003 12.23885 12.20945 12.18252 12.15879 12.13896 12.12374 12.11039
## [113] 12.09591 12.08062 12.06482 12.04883 12.03296 12.01753 12.00284 11.98921
## [121] 11.97696 11.96639 11.95781 11.95155 11.94791 11.94620 11.94552 11.94584
## [129] 11.94713 11.94938 11.95256 11.95664 11.96161 11.96743 11.97409 11.98155
## [137] 11.98981 11.99882 12.00858 12.01904 12.03020 12.04375 12.06116 12.08205
## [145] 12.10607 12.13285 12.16203 12.19323 12.22610 12.26027 12.29538 12.33106
## [153] 12.36694 12.40266 12.43785 12.47216 12.50520 12.53663 12.56607 12.59316
## [161] 12.61754 12.63883 12.65939 12.68166 12.70546 12.73057 12.75680 12.78394
## [169] 12.81179 12.84016 12.86884 12.89763 12.92632 12.95473 12.98264 13.00986
## [177] 13.03619 13.06141 13.08534 13.10777 13.12851 13.14734 13.16407 13.17849
## [185] 13.19042 13.19963 13.20595 13.20915 13.21062 13.21177 13.21249 13.21266
## [193] 13.21216 13.21086 13.20866 13.20542 13.20104 13.19538 13.18834 13.17979
## [201] 13.16962 13.15770 13.14257 13.12317 13.09997 13.07345 13.04408 13.01234
## [209] 12.97871 12.94365 12.90764 12.87116 12.83468 12.79868 12.76364 12.73002
## [217] 12.69830 12.66896 12.63788 12.60124 12.55994 12.51490 12.46703 12.41724
## [225] 12.36645 12.31557 12.26550 12.21718 12.17149 12.12937 12.09172 12.05945
## [233] 12.02785 11.99219 11.95330 11.91205 11.86929 11.82587 11.78265 11.74048
## [241] 11.70021 11.66270 11.62879 11.59935 11.57523 11.55465 11.53528 11.51711
## [249] 11.50015 11.48440 11.46987 11.45657 11.44449 11.43365 11.42404 11.41568
## [257] 11.40856 11.40269 11.39807 11.39471 11.39357 11.39544 11.40008 11.40724
## [265] 11.41667 11.42812 11.44134 11.45608 11.47211 11.48916 11.50699 11.52535
## [273] 11.54400 11.56268 11.58115 11.59916 11.61646 11.63280 11.64794 11.66406
## [281] 11.68323 11.70501 11.72894 11.75458 11.78150 11.80923 11.83735 11.86540
## [289] 11.89293 11.91951 11.94469 11.96802 11.98906 12.00949 12.03118 12.05400
## [297] 12.07778 12.10239 12.12767 12.15349 12.17969 12.20612 12.23265 12.25911
## [305] 12.28538 12.31129 12.33670 12.36146 12.38544 12.40847 12.43042 12.45113
## [313] 12.47047 12.48827 12.50497 12.52111 12.53674 12.55190 12.56663 12.58099
## [321] 12.59502 12.60876 12.62226 12.63556 12.64871 12.66176 12.67474 12.68771
## [329] 12.70045 12.71274 12.72462 12.73611 12.74725 12.75807 12.76860 12.77887
## [337] 12.78891 12.79877 12.80846 12.81802 12.82749 12.83689 12.84625 12.85561
## [345] 12.86453 12.87259 12.87990 12.88657 12.89269 12.89839 12.90375 12.90888
## [353] 12.91390 12.91889 12.92397 12.92924 12.93481 12.94077 12.94692 12.95296
## [361] 12.95889 12.96472 12.97045 12.97609 12.98163 12.98708 12.99243 12.99770
## [369] 13.00289 13.00799 13.01301 13.01795 13.02274 13.02731 13.03169 13.03590
## [377] 13.03997 13.04392 13.04777 13.05155 13.05529 13.05900 13.06271 13.06645
## [385] 13.07024 13.07411 13.07839 13.08334 13.08885 13.09481 13.10112 13.10765
## [393] 13.11431 13.12098 13.12755 13.13392 13.13997 13.14560 13.15070 13.15515
## [401] 13.15885 13.16168 13.16355 13.16433 13.16393 13.16279 13.16143 13.15983
## [409] 13.15797 13.15582 13.15335 13.15055 13.14739 13.14385 13.13990 13.13552
## [417] 13.13069 13.12539 13.11958 13.11243 13.10330 13.09251 13.08035 13.06714
## [425] 13.05317 13.03876 13.02420 13.00981 12.99588 12.98273 12.97066 12.95997
## [433] 12.95097 12.94185 12.93081 12.91814 12.90413 12.88907 12.87325 12.85696
## [441] 12.84049 12.82414 12.80818 12.79292 12.77865 12.76565 12.75422 12.74464
## [449] 12.73721 12.73116 12.72551 12.72026 12.71539 12.71090 12.70678 12.70302
## [457] 12.69960 12.69652 12.69377 12.69133 12.68920 12.68738 12.68584 12.68457
## [465] 12.68358 12.68285 12.68236 12.68212 12.68219 12.68264 12.68347 12.68466
## [473] 12.68622 12.68813 12.69037 12.69296 12.69586 12.69909 12.70262 12.70644
## [481] 12.71056 12.71496 12.71969 12.72478 12.73022 12.73602 12.74216 12.74863
## [489] 12.75542 12.76254 12.76996 12.77769 12.78571 12.79401 12.80260 12.81146
## [497] 12.82055 12.82986 12.83940 12.84918 12.85920 12.86948 12.88003 12.89085
## [505] 12.90196 12.91336 12.92507 12.93709 12.94944 12.96212
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.25, n = 510)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 10.87588 10.93408 10.99131 11.04759 11.10290 11.15724 11.21061 11.26301
##   [9] 11.31444 11.36489 11.41436 11.46285 11.51035 11.55686 11.60239 11.64692
##  [17] 11.69046 11.73301 11.77455 11.81509 11.85464 11.89321 11.93081 11.96744
##  [25] 12.00309 12.03776 12.07146 12.10419 12.13594 12.16671 12.19651 12.22533
##  [33] 12.25318 12.28005 12.30583 12.33040 12.35381 12.37609 12.39728 12.41741
##  [41] 12.43650 12.45461 12.47176 12.48799 12.50332 12.51781 12.53147 12.54434
##  [49] 12.55647 12.56788 12.57828 12.58742 12.59536 12.60218 12.60794 12.61272
##  [57] 12.61658 12.61960 12.62185 12.62339 12.62430 12.62465 12.62450 12.62393
##  [65] 12.62188 12.61739 12.61068 12.60198 12.59151 12.57949 12.56616 12.55173
##  [73] 12.53643 12.52048 12.50411 12.48754 12.47100 12.45471 12.43890 12.42378
##  [81] 12.40959 12.39655 12.38488 12.37304 12.35952 12.34460 12.32855 12.31165
##  [89] 12.29418 12.27642 12.25864 12.24112 12.22413 12.20796 12.19289 12.17918
##  [97] 12.16712 12.15592 12.14464 12.13330 12.12192 12.11053 12.09915 12.08780
## [105] 12.07652 12.06533 12.05424 12.04329 12.03249 12.02188 12.01147 12.00109
## [113] 11.99055 11.97991 11.96924 11.95859 11.94802 11.93757 11.92731 11.91730
## [121] 11.90759 11.89824 11.88930 11.88083 11.87289 11.86430 11.85404 11.84245
## [129] 11.82982 11.81649 11.80277 11.78896 11.77541 11.76241 11.75028 11.73935
## [137] 11.72992 11.72233 11.71687 11.71388 11.71366 11.71523 11.71736 11.72008
## [145] 11.72338 11.72728 11.73178 11.73688 11.74259 11.74892 11.75587 11.76346
## [153] 11.77168 11.78054 11.79005 11.80022 11.81105 11.82255 11.83472 11.84757
## [161] 11.86111 11.87534 11.89210 11.91299 11.93764 11.96571 11.99683 12.03066
## [169] 12.06683 12.10499 12.14479 12.18587 12.22787 12.27044 12.31322 12.35585
## [177] 12.39799 12.43928 12.47935 12.51786 12.55445 12.58876 12.62044 12.64913
## [185] 12.67448 12.69613 12.71373 12.72691 12.73824 12.75030 12.76280 12.77544
## [193] 12.78793 12.79997 12.81125 12.82150 12.83040 12.83766 12.84298 12.84608
## [201] 12.84664 12.84438 12.83723 12.82383 12.80493 12.78128 12.75362 12.72272
## [209] 12.68930 12.65414 12.61797 12.58154 12.54561 12.51092 12.47823 12.44827
## [217] 12.42180 12.39958 12.37705 12.34972 12.31837 12.28382 12.24686 12.20829
## [225] 12.16892 12.12953 12.09094 12.05394 12.01934 11.98792 11.96050 11.93788
## [233] 11.91717 11.89518 11.87225 11.84872 11.82491 11.80116 11.77780 11.75518
## [241] 11.73361 11.71344 11.69499 11.67861 11.66462 11.65257 11.64174 11.63207
## [249] 11.62350 11.61598 11.60942 11.60379 11.59901 11.59502 11.59177 11.58919
## [257] 11.58722 11.58579 11.58486 11.58436 11.58523 11.58836 11.59352 11.60051
## [265] 11.60912 11.61915 11.63039 11.64263 11.65567 11.66930 11.68330 11.69749
## [273] 11.71164 11.72555 11.73902 11.75183 11.76379 11.77468 11.78429 11.79347
## [281] 11.80314 11.81324 11.82372 11.83452 11.84557 11.85683 11.86822 11.87971
## [289] 11.89121 11.90269 11.91407 11.92530 11.93632 11.94797 11.96100 11.97527
## [297] 11.99064 12.00695 12.02407 12.04183 12.06011 12.07874 12.09758 12.11648
## [305] 12.13530 12.15390 12.17211 12.18980 12.20682 12.22301 12.23825 12.25236
## [313] 12.26522 12.27667 12.28740 12.29813 12.30882 12.31939 12.32980 12.33998
## [321] 12.34987 12.35940 12.36853 12.37719 12.38531 12.39285 12.39973 12.40590
## [329] 12.41106 12.41506 12.41801 12.42005 12.42133 12.42196 12.42208 12.42183
## [337] 12.42134 12.42074 12.42016 12.41975 12.41962 12.41992 12.42078 12.42232
## [345] 12.42393 12.42494 12.42543 12.42549 12.42522 12.42469 12.42400 12.42323
## [353] 12.42248 12.42181 12.42133 12.42112 12.42127 12.42186 12.42187 12.42037
## [361] 12.41761 12.41386 12.40936 12.40438 12.39916 12.39397 12.38905 12.38466
## [369] 12.38106 12.37850 12.37723 12.37752 12.37894 12.38087 12.38325 12.38605
## [377] 12.38920 12.39264 12.39634 12.40023 12.40427 12.40840 12.41257 12.41673
## [385] 12.42082 12.42480 12.42944 12.43543 12.44261 12.45080 12.45984 12.46955
## [393] 12.47977 12.49031 12.50102 12.51172 12.52223 12.53240 12.54204 12.55099
## [401] 12.55907 12.56612 12.57196 12.57642 12.57934 12.58156 12.58399 12.58655
## [409] 12.58917 12.59177 12.59426 12.59657 12.59861 12.60031 12.60159 12.60236
## [417] 12.60256 12.60209 12.60089 12.59840 12.59431 12.58885 12.58225 12.57476
## [425] 12.56662 12.55805 12.54931 12.54062 12.53223 12.52438 12.51729 12.51122
## [433] 12.50639 12.50148 12.49511 12.48752 12.47889 12.46945 12.45940 12.44896
## [441] 12.43833 12.42773 12.41736 12.40743 12.39815 12.38974 12.38241 12.37635
## [449] 12.37179 12.36822 12.36498 12.36204 12.35940 12.35703 12.35490 12.35299
## [457] 12.35129 12.34978 12.34843 12.34722 12.34613 12.34514 12.34423 12.34338
## [465] 12.34256 12.34177 12.34096 12.34014 12.33941 12.33892 12.33865 12.33858
## [473] 12.33871 12.33901 12.33947 12.34008 12.34083 12.34169 12.34266 12.34372
## [481] 12.34485 12.34604 12.34735 12.34884 12.35049 12.35231 12.35429 12.35640
## [489] 12.35866 12.36105 12.36356 12.36618 12.36891 12.37174 12.37465 12.37765
## [497] 12.38070 12.38378 12.38690 12.39008 12.39331 12.39662 12.40000 12.40348
## [505] 12.40706 12.41075 12.41456 12.41850 12.42258 12.42681
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")

keeping in case

#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")